// pages/creattravel/creattravel.js
// var WxSearch = require('../../component/wxSearchView/wxSearchView.js');

var book_type; //当前选择的类型
var travel_day = 0; //当前安排的是第n天的行程

var day_item = {
  day: '',
  travel_types: {
    scenery: [],
    restaurant: [],
    market: [],
    casino: [],
    hotel: []
  }
};
var total_ary = [day_item];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
      text: '选择景点',
      imgUrl: '../../images/travelPlanIMG/icon_region@3x.png',
      num: 0
    }, {
      text: '选择餐厅',
      imgUrl: '../../images/travelPlanIMG/icon_restaurant@3x.png',
      num: 0
    }, {
      text: '选择购物商场',
      imgUrl: '../../images/travelPlanIMG/icon_shopping@3x.png',
      num: 0
    }, {
      text: '选择娱乐场所',
      imgUrl: '../../images/travelPlanIMG/icon_amusement@3x.png',
      num: 0
    }, {
      text: '选择酒店',
      imgUrl: '../../images/travelPlanIMG/icon_hotel@3x.png',
      num: 0
    }],
    startDate: '',
    daysAry: [{
      day: '第1天',
      isclick: true,
      id: 1
    }],
    day_index: 0,
    showModalStatus: false,

    searValue: '',
    serchType:{
      text: 'none',
      content:'',
      placehould:'输入内容'
    },

    dropMenuList: [{
        id: 1,
        className: '附近',
        classList: [{
          id: 1,
          itemName: '默认'
        }, {
          id: 2,
          itemName: '500米'
        }]
      },
      {
        id: 2,
        className: '分类',
        classList: [{
          id: 1,
          itemName: '全部景点'
        }, {
          id: 2,
          itemName: '风景区'
        }, {
          id: 3,
          itemName: '文博院管'
        }, {
          id: 4,
          itemName: '寺庙观堂'
        }, {
          id: 5,
          itemName: '旅游度假区'
        }]
      }, {
        id: 3,
        className: '排序',
        classList: [{
          id: 1,
          itemName: '智能排序'
        }, {
          id: 2,
          itemName: '距离优先'
        }, {
          id: 3,
          itemName: '人气优先'
        }, {
          id: 4,
          itemName: '好评优先'
        }]
      }
    ],
    dropMenuNum: 0,
    subMenuHighLight: [],
    selected: [],

    store_list: [{
        id: 1,
        storeName: '丰泽面线糊店',
        per_consumption: '18.00',
        pingfen: 4.9,
        juli: '300m',
        imgUrl: '../../images/big_avatar.png',
        selected: false
      },
      {
        id: 2,
        storeName: '泉州咸饭铺子',
        per_consumption: '20.00',
        pingfen: 4.5,
        juli: '510m',
        imgUrl: '../../images/big_avatar.png',
        selected: false
      },
      {
        id: 3,
        storeName: '潇潇牛肉',
        per_consumption: '16.00',
        pingfen: 3.5,
        juli: '1.1km',
        imgUrl: '../../images/big_avatar.png',
        selected: false
      },
      {
        id: 4,
        storeName: '泉州排骨店',
        per_consumption: '19.00',
        pingfen: 4.5,
        juli: '350m',
        imgUrl: '../../images/big_avatar.png',
        selected: false
      },
    ],

    showModal_searchTravel: false,

    booked_travelList:{
      id: 1,
      day: '第一天',
      travel: [{
        text: '景点',
        imgurl:'../../images/travelPlanIMG/icon_region@3x.png',
        places: []
      }, {
        text: '餐厅',
        imgurl: '../../images/travelPlanIMG/icon_restaurant@3x.png',
        places: []
      }, {
        text: '商场',
        imgurl: '../../images/travelPlanIMG/icon_shopping@3x.png',
        places: []
      }, {
        text: '娱乐场所',
        imgurl: '../../images/travelPlanIMG/icon_amusement@3x.png',
        places: []
      }, {
        text: '酒店',
        imgurl: '../../images/travelPlanIMG/icon_hotel@3x.png',
        places: []
      }],
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.getData();

    this.initDropDown();
  },
  //初始化下拉选择的数据
  initDropDown:function(){
    var list = this.data.dropMenuList;

    var ary_outer = [];
    var ary_select = [];
    for (var i = 0; i < list.length; i++) {

      var ary_inner = [];
      for (var j = 0; j < list[i].classList.length; j++) {
        var obj;

        obj = '';
        if (j == 0) {
          obj = 'highlight';
        }

        ary_inner.push(obj);
      }

      ary_outer.push(ary_inner);

      var str = i + '-0';
      ary_select.push(str);

    }
    
    this.setData({
      dropMenuNum: list.length,
      subMenuHighLight: ary_outer,
      selected: ary_select
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  //获取当前时间
  getData: function() {
    var date = new Date();
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate();

    var r = year + '-' + month + '-' + day
    this.setData({
      startDate: r
    })
  },
  //选择出行日期
  bindDateChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      startDate: e.detail.value
    })
  },

  //切换日期行程
  positionTap: function(e) {

    console.log(e.currentTarget.dataset.index);

    var i = e.currentTarget.dataset.index;

    this.switchDay(i);

    //处理徽标数
    this.getBardge();
  },
  //处理切换日期行程
  switchDay:function(index_d){
    var that = this;
    for (var i = 0; i < this.data.daysAry.length; i++) {
      var obj = this.data.daysAry[i];
      var index = obj.id - 1;
      var select = "daysAry[" + index + "].isclick";
      that.setData({
        [select]: false
      })
    }
    var index = index_d;
    var select = "daysAry[" + index + "].isclick";
    that.setData({
      [select]: true
    })

    travel_day = index;
  },
  //处理徽标数
  getBardge: function() {
    var item = total_ary[travel_day].travel_types;
    var ary = [];
    for (var obj in item) {
      console.log(obj);
      ary.push(item[obj].length);
    }
    console.log(ary);
    var list = this.data.list;
    for (var i = 0; i < ary.length; i++) {
      list[i].num = ary[i]
    }
    this.setData({
      list: list
    })
  },
  //添加旅游行程
  addTravelDay: function() {
    var i = this.data.day_index;
    i = i + 1;
    var ary = this.data.daysAry;

    var obj = {
      day: '第' + (i + 1) + '天',
      isclick: false,
      id: i + 1
    }
    ary.push(obj);

    this.setData({
      day_index: i,
      daysAry: ary
    })


    var obj_day = {
      day: '第' + (i + 1) + '天',
      travel_types: {
        scenery: [],
        restaurant: [],
        market: [],
        casino: [],
        hotel: []
      }
    };
    total_ary.push(obj_day); //总行程上添加一条新的行程
  },
  // 选择 景点/餐厅/购物商场/娱乐场所/酒店
  choiceTap: function(e) {

    var index = e.currentTarget.dataset.index;

    switch (index) {
      case 0:
        // 景点
        book_type = 'scenery';
        break;
      case 1:
        // 餐厅
        book_type = 'restaurant';
        break;
      case 2:
        //购物商场
        book_type = 'market';
        break;
      case 3:
        //娱乐场所
        book_type = 'casino';
        break;
      case 4:
        //酒店
        book_type = 'hotel';
        break;
    }
    console.log(this.data.store_list, total_ary, travel_day);

    //重新请求推荐列表数据
    var list = this.data.store_list;

    var ary = total_ary[travel_day].travel_types[book_type];

    console.log('全部', list, ary, '选好的');

    for (var i = 0; i < list.length; i++) {
      var obj = list[i];
      if (ary.length < 1) {
        obj.selected = false;
      } else {
        obj.selected = false;
        for (var j = 0; j < ary.length; j++) {

          var item = ary[j];
          if (obj.id == item.id) {
            console.log(obj.id);
            obj.selected = true;

          }
        }
      }

    }
    this.setData({
      store_list: list
    })

    this.showModal('showModalStatus');
  },
  showModal: function(e) {

    //  console.log(e);
    var show;
    if (e == 'showModalStatus') {

      show = 'showModalStatus';
    } else {
      show = 'showModal_searchTravel';
    }

    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      [show]: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)

    if (show == 'showModal_searchTravel'){
      
      this.dayData(travel_day);
      console.log(this.selectComponent("#collapse"));
    }
  },
  hideModal: function(e) {
    var show;
    if (e == 'showModalStatus') {
      show = 'showModalStatus';
    } else if (e == 'showModal_searchTravel') {
      show = 'showModal_searchTravel';
    } else {
      show = e.currentTarget.dataset.show;
    }

    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        [show]: false
      })
    }.bind(this), 200)

    if (show == 'showModal_searchTravel') {
      this.setData({

      })
    }
  },
  //模态框 点击日期操作
  dayTap:function(e){

    var index = e.currentTarget.dataset.index;
    
    this.switchDay(index);

    //处理徽标数
    this.getBardge();

    this.dayData(index);
  },
  //模态框 点击删除操作
  del_travelItem:function(prames){
    console.log(prames.detail);

    var parent = prames.detail.parent;
    var child = prames.detail.index;

    var currentItem;

    switch (parent) {
      case 0:
        // 景点
        currentItem = 'scenery';
        break;
      case 1:
        // 餐厅
        currentItem = 'restaurant';
        break;
      case 2:
        //购物商场
        currentItem = 'market';
        break;
      case 3:
        //娱乐场所
        currentItem = 'casino';
        break;
      case 4:
        //酒店
        currentItem = 'hotel';
        break;
    }
    var ary = total_ary[travel_day].travel_types[currentItem];

    console.log(travel_day, currentItem, child);

    ary.splice(child, 1);

    this.dayData(travel_day);
    //处理徽标数
    this.getBardge();
    
  },
  //模态框 点击切换操作
  dayData:function(i){

    console.log(total_ary);

    var item = total_ary[i];

    console.log(item);

    var obj = this.data.booked_travelList;
    obj.day = item.day;
    obj.travel[0].places = item.travel_types.scenery;
    obj.travel[1].places = item.travel_types.restaurant;
    obj.travel[2].places = item.travel_types.market;
    obj.travel[3].places = item.travel_types.casino;
    obj.travel[4].places = item.travel_types.hotel;

    this.setData({
      booked_travelList: obj
    })
    var that = this;

    that.selectComponent("#collapse").initSubMenuDisplay();
    that.selectComponent("#collapse").initTouchMove();

    
  },
  onGetValue: function(e) {
    console.log('输入框传值', e.detail);
  },
  selectIndex: function(e) {
    console.log('选中的索引值', e.detail);
  },
  //确定选择景点
  confirm_select: function(e) {
    //参数1：第几天 参数2：景点/餐厅/购物商场/娱乐场所/酒店

    console.log(book_type, travel_day);

    var day = travel_day + 1;
    total_ary[travel_day].day = '第' + day + '天';

    this.hideModal('showModalStatus');

    //处理徽标数
    this.getBardge();
  },
  //确认出行计划
  confirm_plan: function() {
    this.hideModal('showModal_searchTravel');
  },
  //查看模态框景点详情
  orderDetail: function(e) {

  },

  //点击加号 添加景点等
  addItem: function(e) {

    var that = this;

    var index = e.currentTarget.dataset.index;

    var store_list = this.data.store_list;

    //修改样式
    var str = 'store_list[' + index + '].selected';
    that.setData({
      [str]: true
    })
    //修改数据
    var obj = store_list[index];

    console.log(index, obj);

    total_ary[travel_day].travel_types[book_type].push(obj);

    console.log(total_ary, 'addItem');

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})